<?php
defined('_JEXEC') or die;
jimport('joomla.event.plugin');

/**
 * Syntax Highlighter - Joomla Plugin to handle rendering of button and javascript required 
 * and integrating into Joomla's Text Editor
 * 
 * @author Brian Bolli
 *
 */
class plgButtonBPSyntaxHighlighter extends JPlugin {
	
	/**
	* Constructor
	*
	* @access      protected
	* @param       object  $subject The object to observe
	* @param       array   $config  An array that holds the plugin configuration
	* @since       1.5
	*/
	public function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage();
	}
	
	/**
	* Display Button with Joomla Text Editor
	*
	* @return array A two element array of (snippetName, textToInsert)
	*/
	function onDisplay($name)
	{
		/*
		 * Javascript to insert the link
		 * View element calls jSelectArticle when an article is clicked
		 * jSelectArticle creates the link tag, sends it to the editor,
		 * and closes the select frame.
		 */
		$js = "
		function jSelectSnippet(id, title, catid, object) {
			var tag = '{LoadSnippet ' + id + '}';
			jInsertEditorText(tag, '".$name."');
			SqueezeBox.close();
		}";

		$doc = &JFactory::getDocument();
		$doc->addScriptDeclaration($js);

		JHtml::_('behavior.modal');
		$link = 'index.php?option=com_bpsyntaxhighlighter&amp;view=snippets&amp;layout=modal&amp;tmpl=component&amp;'.JSession::getFormToken().'=1';
		$button = new JObject();
		$button->set('modal', true);
		$button->set('link', $link);
		$button->set('text', JText::_('Code Snippet'));
		$button->set('name', 'article');
		$button->set('options', "{handler: 'iframe', size: {x: 770, y: 400}}");
		return $button;
	}
}